package com.springboot.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.common.CustomException;
import com.springboot.dao.SetmealMapper;
import com.springboot.pojo.Setmeal;
import com.springboot.pojo.SetmealDish;
import com.springboot.pojo.SetmealDto;
import com.springboot.service.SetmealDishService;
import com.springboot.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * 新增套餐同时保存菜品和套餐关系
     * @param setmealDto
     */
    @Transactional
    @Override
    public void setmealWithDish(SetmealDto setmealDto) {
        //保存菜品基本信息，操作Setmeal,执行insert操作
          this.save(setmealDto);
        //保存菜品和套餐的关联信息，操作SetmealDish,执行insert操作
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
             item.setSetmealId(setmealDto.getId());
             return item;
         }).collect(Collectors.toList());
         setmealDishService.saveBatch(setmealDishes);

    }
    /**
     * 根据id删除套餐
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {
         //查询套餐状态
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
         wrapper.in(Setmeal::getId,ids);
         wrapper.eq(Setmeal::getStatus,1);
        int count = this.count(wrapper);
        //如果为起售状态，则不能删除
        //如果不能删除，则抛出异常
        if (count>0){
            throw  new CustomException("该套餐已起售，不能删除");
        }
        this.removeByIds(ids);
        //删除关系表中数据
         LambdaQueryWrapper<SetmealDish> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.in(SetmealDish::getSetmealId,ids);
            setmealDishService.remove(wrapper2);
    }
    /**
     * 根据id更新套餐状态
     * @return
     */
    @Override
    public List<Setmeal> setstatus(int status, List<Long> ids) {
        List<Setmeal> list=null;
         if (status==1){
             log.info("开始更新套餐状态为起售");
             Setmeal setmeal = new Setmeal();
             setmeal.setStatus(0);
             //起售状态，则更新套餐状态为0停售
             LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper();
             wrapper.in(Setmeal::getId,ids);
           //查询套餐状态为1的套餐
               list = this.list(wrapper);
             //steam流操作，更新状态
              list= list.stream().map((item)->{
                item.setStatus(0);
                 return item;
             }).collect(Collectors.toList());
             //更新套餐状态为0
             this.updateBatchById(list);

         }else if (status==0){
             log.info("开始更新套餐状态为停售");
             //停售状态，则更新套餐状态为1起售
             LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper();
             wrapper.in(Setmeal::getId,ids);
             list= this.list(wrapper);
             list=list.stream().map((item)->{
                 item.setStatus(1);
                 return item;
             }).collect(Collectors.toList());
             list.stream().map((item)->{
                 this.updateById(item);
                 return item;
             }).collect(Collectors.toList());
         }
     return  list;
    }
}
